// src/api/chat/message.ts
import  {request } from '@/utils/request'

export type MsgType = 'text' | 'image' | 'file' | 'audio' | 'video'

export interface ChatMessage {
  id?: number
  sessionId?: number
  senderId: number
  msgType: string
  content?: string
  payloadUrl?: string
  payloadMeta?: string // JSON 字符串
  qos?: 0 | 1 | 2
  retained?: 0 | 1
  delivered?: 0 | 1
  createdAt?: string
  updatedAt?: string
}

export interface SendMessageParams {
  targetUserId?: number
  targetSessionId?: number
}

export interface HistoryParams {
  sessionId: number
  page: number
  size: number
}

export interface ReadParams {
  id: number
  readerId: number
}

/**
 * 发送消息（代发/持久化）
 * POST /api/chat/message/send
 * @param {SendMessageParams} params 可选：{ targetUserId?, targetSessionId? }
 * @param {ChatMessage} data 消息体
 * @returns {Promise<any>} 响应：{"code":0,"message":"success","data":ChatMessage}
 */
export function sendMessage(params: SendMessageParams, data: ChatMessage): Promise<any> {
  return request({
    url: '/api/chat/message/send',
    method: 'post',
    params,
    data,
  })
}

/**
 * 历史消息
 * GET /api/chat/message/history
 * @param {HistoryParams} params { sessionId, page>=1, size }
 * @returns {Promise<any>} 响应：{"code":0,"message":"success","data":ChatMessage[]}
 */
export function getMessageHistory(params: HistoryParams): Promise<any> {
  return request({
    url: '/api/chat/message/history',
    method: 'get',
    params,
  })
}

/**
 * 标记已读
 * POST /api/chat/message/{id}/read
 * @param {ReadParams} dataOrParams { id, readerId }（采用 params 传递 readerId）
 * @returns {Promise<any>} 响应：{"code":0,"message":"success","data":"ok"}
 */
export function markMessageRead(id: number | string, readerId: number): Promise<any> {
  return request({
    url: `/api/chat/message/${id}/read`,
    method: 'post',
    params: { readerId },
  })
}